﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace xycn.common
{
    /// <summary>
    /// Xml文件配置库
    /// </summary>
    public class XmlConfigHelper
    {

        public static XmlDocument _xmldoc = new XmlDocument();

        public static XmlDocument Xmldoc
        {
            get
            {
                if (!_xmldoc.HasChildNodes)
                {
                    //获取当前运行的程序名称
                    var fi =
                        System.Diagnostics.Process.GetCurrentProcess().MainModule.FileVersionInfo;

                    var xmlpath = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.GetFileName(fi.FileName) + ".xml";

                    _xmldoc.Load(xmlpath);
                }
                return _xmldoc;
            }
        }

        /// <summary>
        /// 查询配置，返回值 无 返回null 一个返回值 多个返回list
        /// </summary>
        /// <param name="nodeName">xpath路径   /Root/xxx/yyy/zzz </param>
        /// <returns></returns>
        public static object GetConfigValue(string nodeName)
        {
            var docRoot = Xmldoc.DocumentElement;
            var nodes = docRoot?.SelectNodes(nodeName);

            if (nodes != null && nodes.Count > 0)
            {
                List<string> res = new List<string>();
                foreach (XmlNode item in nodes)
                {
                    res.Add(item.InnerText);
                }
                if (res.Count == 1)
                {
                    return res[0];
                }
                return res;
            }
            return null;
        }

        /// <summary>
        /// 查询配置，返回字符串
        /// </summary>
        /// <param name="key">键名 支持 xpath路径 如  /Config/DBService/ConnString </param>
        /// <returns>捕获异常,当未设置key时,返回空字符串</returns>
        public static string GetConfigToString(string key)
        {
            try
            {
                var obj = GetConfigValue(key);
                if (obj != null)
                {
                    return obj.ToString();
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 查询配置，返回整数
        /// </summary>
        /// <param name="key">键名 支持 xpath路径 如  /Config/DBService/ConnString </param>
        /// <returns>捕获异常,当未设置key 或key不是数字 时,返回 int 最小值</returns>
        public static int GetConfigToInt(string key)
        {
            object obj = GetConfigValue(key);
            if (obj == null)
            {
                return int.MinValue;
            }
            else
            {
                try
                {
                    return Convert.ToInt32(obj);
                }
                catch
                {
                    return int.MinValue;
                }

            }
        }

        /// <summary>
        /// 查询配置，返回长整数
        /// </summary>
        /// <param name="key">键名 支持 xpath路径 如  /Config/DBService/ConnString </param>
        /// <returns>不会捕获异常,当未设置key 或key不是数字 时,返回 长整数 最小值</returns>
        public static long GetConfigToLong(string key)
        {
            object obj = GetConfigValue(key);
            if (obj == null)
            {
                return long.MinValue;
            }
            else
            {
                try
                {
                    return Convert.ToInt64(obj);
                }
                catch
                {
                    return long.MinValue;
                }

            }
        }

        /// <summary>
        /// 查询配置，返回日期
        /// </summary>
        /// <param name="key">键名 支持 xpath路径 如  /Config/DBService/ConnString </param>
        /// <returns>不会捕获异常</returns>
        public static DateTime GetConfigToDateTime(string key)
        {
            object obj = GetConfigValue(key);

            if (obj == null)
            {
                return DateTime.MinValue;
            }
            else
            {
                try
                {
                    return Convert.ToDateTime(GetConfigValue(key));
                }
                catch (Exception)
                {
                    return DateTime.MinValue;
                }
            }
        }

        /// <summary>
        /// 查询配置，返回逻布尔值
        /// </summary>
        /// <param name="key">键名 支持 xpath路径 如  /Config/DBService/ConnString </param>
        /// <returns>不会捕获异常</returns>
        public static bool GetConfigToBool(string key)
        {
            object obj = GetConfigValue(key);
            if (obj == null)
            {
                return false;
            }
            else
            {
                try
                {
                    return Convert.ToBoolean(GetConfigValue(key));
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 应用程序当前目录 包含最后一个目录分隔符号
        /// </summary>
        public static string BaseDirectory
        {
            get
            {
                string path = System.AppDomain.CurrentDomain.BaseDirectory;
                if (!path.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
                {
                    path += System.IO.Path.DirectorySeparatorChar.ToString();
                }
                return path;
            }
        }

        /// <summary>
        /// 临时目录（当前程序下的temp子目录） 包含最后一个目录分隔符号
        /// </summary>
        public static string TempFilePath
        {
            get
            {
                string path = BaseDirectory + "temp" + System.IO.Path.DirectorySeparatorChar;
                if (!System.IO.Directory.Exists(path))
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    catch
                    {
                        return string.Empty;
                    }
                }

                return path;
            }
        }

        /// <summary>
        /// 将字符串读取出来转换成List
        /// </summary>
        /// <param name="key">配置文件Key</param>
        /// <param name="splitChar">节点下多条</param>
        /// <returns></returns>
        public static List<string> GetConfigToList(string key)
        {
            var obj = GetConfigValue(key);

            return obj as List<string>;
        }
    }
}
